import * as areaService from '@/services/area';
import * as eventService from '@/services/event';
import * as sendSmsService from '@/services/sendSms';
import { message } from 'antd';
import { DEFAULT_RESPONE_SUCCESS } from '@/constants';

export default {
  namespace: 'emergency',

  state: {
    current: 1,
    total: 0,
    data: [],
    rowKey: 'id'
  },

  subscriptions: {

  },

  effects: {
    *queryAreaTree({ payload }, { call, put }) {
      const { callback, isLeaf = true, ...extra } = payload;

      const response = yield call(areaService.queryData, extra);
      if (response && response.status === DEFAULT_RESPONE_SUCCESS) {
        const { result = [] } = response;

        if (typeof callback === 'function') {
          callback(result.map(item => ({ ...item, isLeaf })));
        } else {
          yield put({
            type: 'changeState',
            payload: { areas: result.map(item => ({ ...item, isLeaf })) }
          });
        }
      }
    },
    *queryData({ payload }, { call, put }) {
      const { callback, current = 1, pageSize, ...extra } = payload;

      const response = yield call(eventService.queryData, { pageNo: current, pageSize }, { ...extra });
      if (response && response.status === DEFAULT_RESPONE_SUCCESS) {
        const { result = {} } = response;
        const { content = [], totalElements: total = 0 } = result;

        yield put({
          type: 'changeState',
          payload: {
            data: content,
            current,
            pageSize,
            total: typeof total === 'string' ? parseInt(total) : total
          }
        });
      }
    },
    *saveOrUpdateData({ payload }, { call, put }) {
      const { data, ...extra } = payload;
      const option = {
        method: 'POST',
        body: [data]
      };
      let response = {};
      if (!data.id) {
        response = yield call(eventService.saveData, option);
      } else {
        response = yield call(eventService.updateData, option);
      }
      if (response && response.status === DEFAULT_RESPONE_SUCCESS) {
        message.success(!data.id ? '新增突发事件成功' : '编辑突发事件成功', 1);
        yield put({
          type: 'changeState',
          payload: { visible: false, editData: {} }
        });
        yield put({ type: 'queryData', payload: { ...extra } });
      }
    },
    *deleteData({ payload }, { call, put }) {
      const { ids, ...extra } = payload;
      const option = {
        method: 'POST',
        body: ids
      };
      const response = yield call(eventService.deleteData, option);
      if (response && response.status === DEFAULT_RESPONE_SUCCESS) {
        message.success('删除突发事件成功', 1);
        yield put({ type: 'queryData', payload: { ...extra } });
      }
    },
    *toggleTop({ payload }, { call, put }) {
      const { id, state, ...extra } = payload;
      const option = {
        method: 'POST',
        body: [id]
      };
      const response = yield call(eventService.topData, { state }, option);
      if (response && response.status === DEFAULT_RESPONE_SUCCESS) {
        yield put({ type: 'queryData', payload: { ...extra } });
      }
    },
    *queryEventProcess({ payload }, { call, put }) {
      const { eventId, callback } = payload;

      const response = yield call(eventService.queryEventProcess, { pageNo: 1, pageSize: 10000 }, { eventId });
      if (response && response.status === DEFAULT_RESPONE_SUCCESS) {
        const { result = {} } = response;
        const { content = [], totalElements: total = 0 } = result;

        if (typeof callback === 'function') {
          callback(content);
        }
      }
    },
    *saveOrUpdateEventProcess({ payload }, { call, put, select }) {
      const { data: list = [] } = yield select(state => state.emergency);
      const { data, ...extra } = payload;
      const option = {
        method: 'POST',
        body: [data]
      };

      let response = {};
      if (!data.id) {
        response = yield call(eventService.saveEventProcess, option);
      } else {
        response = yield call(eventService.updateEventProcess, option);
      }
      if (response && response.status === DEFAULT_RESPONE_SUCCESS) {
        message.success(!data.id ? '新增流程记录成功' : '编辑流程记录成功', 1);
        if (!data.id) {
          yield put({
            type: 'changeState',
            payload: {
              data: list.map(item => {
                if (item.id === extra.eventId) {
                  return { ...item, processCount: item.processCount + 1 };
                }
                return { ...item };
              })
            }
          });
        }
        yield put({ type: 'queryEventProcess', payload: { ...extra } });
      }
    },
    *deleteEventProcess({ payload }, { call, put, select }) {
      const { data: list = [] } = yield select(state => state.emergency);
      const { ids, eventId, ...extra } = payload;
      const option = {
        method: 'POST',
        body: ids
      };

      const response = yield call(eventService.deleteEventProcess, option);
      if (response && response.status === DEFAULT_RESPONE_SUCCESS) {
        message.success('删除流程记录成功', 1);

        yield put({
          type: 'changeState',
          payload: {
            data: list.map(item => {
              if (item.id === eventId) {
                return { ...item, processCount: item.processCount - 1 };
              }
              return { ...item };
            })
          }
        });
        yield put({ type: 'queryEventProcess', payload: { ...extra } });
      }
    },
    *queryEventPush({ payload }, { call, put }) {
      const { pushUnitId, callback } = payload;

      const response = yield call(eventService.queryEventPush, { pageNo: 1, pageSize: 10000 }, { pushUnitId });
      if (response && response.status === DEFAULT_RESPONE_SUCCESS) {
        const { result = {} } = response;
        const { content = [], totalElements: total = 0 } = result;

        if (typeof callback === 'function') {
          callback(content);
        }
      }
    },
    *saveOrUpdateEventPush({ payload }, { call, put, select }) {
      const { list, callback } = payload;
      const option = {
        method: 'POST',
        body: list
      };

      const response = yield call(eventService.saveEventPush, option);
      if (response && response.status === DEFAULT_RESPONE_SUCCESS) {
        message.success('推送成功', 1);

        if (typeof callback === 'function') {
          callback();
        }
      }
    },
    *sendSms({ payload }, { call, put }) {
      const { data, callback } = payload;
      const option = {
        method: 'POST',
        body: data
      };

      const response = yield call(sendSmsService.saveData, option);
      if (response && response.status === DEFAULT_RESPONE_SUCCESS) {
        if (typeof callback === 'function') {
          message.success('操作成功', 1);

          callback();
        }
      }
    }
  },

  reducers: {
    clear(state, { payload }) {
      return {
        current: 1,
        total: 0,
        data: [],
        rowKey: 'id'
      };
    },
    changeState(state, { payload }) {
      return {
        ...state,
        ...payload
      };
    }
  },
};
